package cn.gson.crm.model.service;

import cn.gson.crm.model.dao.CustomerServiceDao;
import cn.gson.crm.model.mapper.CustomerServiceMapper;
import cn.gson.crm.model.mapper.UserMapper;
import cn.gson.crm.model.pojos.*;
import cn.gson.crm.model.pojos.CustomerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class CustomerService_Service {
    @Autowired
    CustomerServiceMapper customerServiceMapper;
    @Autowired
    CustomerServiceDao customerServiceDao;
    @Autowired
    UserMapper userMapper;
    //通用方法查询当前登录人是否为老板
    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=userMapper.selectctlaofou(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return null;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }
    //查询当前人所有客户投诉记录
    public Map<String,Object> selectcom(HttpSession session,Integer cid, Integer page, Integer size){
        cid=fouyuan(session);
        List<cn.gson.crm.model.pojos.CustomerService> complaints2=customerServiceMapper.selectcom(cid);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<cn.gson.crm.model.pojos.CustomerService> complaints=customerServiceMapper.selectcom(cid);
        Map<String,Object> map = new HashMap<>();
        User login = (User)session.getAttribute("login");
        int ygId= (int) login.getEmployees().get(0).getYgId();
        List<Customer>  customerList=customerServiceMapper.selectcustomer(ygId);
        map.put("total",obj.getTotal());
        map.put("customerList",customerList);
        map.put("rows",complaints);
        map.put("tong",complaints2);
        return map;
    };
    //条件查询
    public Map<String,Object> selectwhere( HttpSession session,Integer cid, String where,Integer page, Integer size){
        cid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<cn.gson.crm.model.pojos.CustomerService> complaints=customerServiceMapper.selectwhere(cid,where);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",complaints);
        return map;
    };
    //根据时间查询
    public Map<String,Object> selecttime( HttpSession session,Integer cid, String statr, String endtime,Integer page, Integer size){
        cid=fouyuan(session);
        Page<Object> obj = PageHelper.startPage(page, size);
        List<cn.gson.crm.model.pojos.CustomerService> complaints=customerServiceMapper.selecttime(cid,statr,endtime);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",complaints);
        return map;
    };
    //新增
    public void  savecom(CustomerService customerService){
        customerServiceDao.save(customerService);
    }
    //删除
    public void deletCustomerService(CustomerService customerService){
        customerServiceDao.delete(customerService);
    }

}
